#include <iostream>

 
  struct ListNode {
      int val;
      ListNode *next;
    ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };
 
ListNode* reverseList(ListNode* head) {
    if (head == nullptr) return head;
    ListNode* pre,* now,* nex;
    pre = nullptr;
    now = head;
    nex = now->next;
    while ( now != nullptr) {
        nex = now->next;
        now->next = pre;
        pre = now;
        now = nex;
    }
    return pre;
}
